/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    ActivityIndicator,
    AsyncStorage
} from 'react-native';
import SplashScreen from 'react-native-splash-screen'
import Toast from 'react-native-root-toast';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import store from './createStore'
import Storage from 'react-native-storage';
import AppNavigator from './modules/navigator/Navigator'
import * as fetch from './utils/fetch';
// import * as storage from './utils/storage';
import * as NavigatorService from './utils/NavigatorService';
import Loading from './views/loading';
import RfidModule from './components/Rfid';
import o from './components/o'

import { login, goLogin, loginSuccess } from './actions/LoginAction';
class ERP extends Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
        SplashScreen.hide();
        const DAFAULTEXPIRES = 1000 * 3600 * 24;
        let storage = new Storage({
            // maximum capacity, default 1000 
            size: 1000,
            // Use AsyncStorage for RN, or window.localStorage for web.
            // If not set, data would be lost after reload.
            storageBackend: AsyncStorage,

            // expire time, default 1 day(1000 * 3600 * 24 milliseconds).
            // can be null, which means never expire.
            defaultExpires: DAFAULTEXPIRES,

            enableCache: true,

            sync: {
            }
        })
        global.storage = storage;
        storage.load({
            key: "openid"
        }).then(res => {
            this.props.loginSuccess({openid:res})
        }).catch(e => {
            console.log(e)
            this.props.goLogin()
        })
    }

    componentWillUnmount() {
        //在删除后台后应该删除用户信息 下次进入APP保证用户信息是最新的
        storage.remove("userInfo")
        RfidModule.release();//释放模块上电掉电控制设备
    }
    _testLogin() {

    }
    _testLogin2() {
        fetch.request('/rest/auth/login?user=admin&passwd=bWFuYWdl', 'get').then((data) => {
            console.log(data)
            if (data.token) {
                global.isLogined = true;
                storage.save('token', data.token);
                //用与获取用户信息的openid应该存储在本地 当用户点击退出才清除
                const userInfo = {

                }
                this.setState({
                    loading: false //确定登录状态后取消loading
                })
            } else {
                global.isLogined = false;
                this.setState({
                    loading: false
                })
            }
        }).catch(e => {
            console.log(e)
            Toast.show("登录异常", {
                position: Toast.positions.CENTER,
                hideOnPress: false
            })
        })
    }

    render() {
        return (
            this.props.loading ?
                <Loading /> :
                <AppNavigator
                    ref={navigatorRef => {
                        NavigatorService.setContainer(navigatorRef);
                    }} />
        );
    }
}
function select(store) {
    return {
        loding: store.User.isLoading
    }
}
// dispatch => ({ login: bindActionCreators(login, dispatch) },{ goLogin: bindActionCreators(goLogin, dispatch) }, { getToken: bindActionCreators(getToken, dispatch) })
export default connect(
    state => ({ loading: state.User.isLoading }),
    dispatch => ({ login: bindActionCreators(login, dispatch), goLogin: bindActionCreators(goLogin, dispatch), loginSuccess: bindActionCreators(loginSuccess, dispatch) })
)(ERP)




